package com.thoughtworks.conference.service.impl;

import com.thoughtworks.conference.constants.CommonConstants;
import com.thoughtworks.conference.dao.ConferenceTrackDao;
import com.thoughtworks.conference.dao.impl.ConferenceTrackDaoImpl;
import com.thoughtworks.conference.model.SessionTalkInfo;
import com.thoughtworks.conference.model.TalkInfo;
import com.thoughtworks.conference.model.TrackInfo;
import com.thoughtworks.conference.model.TrackSession;
import com.thoughtworks.conference.service.ConferenceTalkService;
import com.thoughtworks.conference.service.ConferenceTrackService;
import com.thoughtworks.conference.utils.ConferenceUtils;
import com.thoughtworks.conference.utils.TalkInfloCompare;

import java.util.*;

/**
 * <p>Title: 会议跟踪的业务处接口实现类</p>
 * <p>Description:实现会议跟踪的处理和打印</p>
 *
 * @author <a href="mailTo:bingxuewulei@outlook.com">wanglei</a>
 * @version 1.0
 * @history: Created by wanglei on  2019/4/23
 */
public class ConferenceTrackServiceImpl implements ConferenceTrackService {
    ConferenceTalkService conferenceTalkService = new ConferenceTalkServiceImpl();
    ConferenceTrackDao conferenceTrackDao = new ConferenceTrackDaoImpl();
    @Override
    public List<TrackInfo> dealConferenceTrack(String filePath) {
        List<TalkInfo> talkInfoList = conferenceTalkService.feachTalkInfo(filePath);
        //按照会议时长降序排序
        talkInfoList.sort(new TalkInfloCompare());
        int trackCount = 0;
        List<TrackInfo> trackInfoList = new ArrayList<>();
        TrackInfo tempTrackInfo ;
        // 循环处理会议列表直到所有的会议被安排完
        while (0 != talkInfoList.size()) {
            // 安排早上要进行的会议
            TrackSession morningSession = new TrackSession(CommonConstants.SESSION_START_TIME,
                    CommonConstants.MORNING_SESSION_MINUTES);
            morningSession.setMorningSessionTalkInfo(getSessionTalkInfo(talkInfoList,morningSession ));
            // 安排午饭时间
            SessionTalkInfo lunchTrackInfo = new SessionTalkInfo(CommonConstants.LUNCH_SESSION_TIME,
                    CommonConstants.LUNCH_STRING);
            TrackSession lunchSession = new TrackSession(CommonConstants.LUNCH_SESSION_TIME);
            lunchSession.setLunchTalkInfo(lunchTrackInfo);
            // 安排下午的会议
            TrackSession afternoonSession = new TrackSession(CommonConstants.AFTRNOON_SESSION_START_TIME,
                    CommonConstants.AFTERNOON_SESSION_MINUTES);
            afternoonSession.setAfterNoonSessionTalkInfo(getSessionTalkInfo(talkInfoList,afternoonSession ));
            SessionTalkInfo netWorkTrackInfo = new SessionTalkInfo(CommonConstants.NETWORK_SESSION_TIME,
                    CommonConstants.NET_WORK_STRING);
            // 安排网络活动
            TrackSession netWorkSession = new TrackSession(CommonConstants.NETWORK_SESSION_TIME);
            netWorkSession.setLunchTalkInfo(netWorkTrackInfo);
            tempTrackInfo = new TrackInfo(CommonConstants.TRACK_STRING + ++trackCount);
            tempTrackInfo.setMorningTrackSession(morningSession);
            tempTrackInfo.setLunchSession(lunchSession);
            tempTrackInfo.setAfterNoonTrackSession(afternoonSession);
            tempTrackInfo.setNetWorkSession(netWorkSession);
            trackInfoList.add(tempTrackInfo);
        }
        printConferenceTrack(trackInfoList);
        return trackInfoList ;
    }

    @Override
    public void printConferenceTrack( List<TrackInfo> trackInfoList) {
        conferenceTrackDao.printConferenceTrackInfo(trackInfoList);
    }

    @Override
    public List<TrackInfo> dealConferenceTrack2(String filePath) {
        return null;
    }

    /**
     * 把每个会议安排到早上和下午的时间段内
     * @param talkInfoList 会议信息列表
     * @param trackSession 会议跟踪的时间段
     * @return
     */
    private List<SessionTalkInfo> getSessionTalkInfo(List<TalkInfo> talkInfoList, TrackSession trackSession){
        //获取当前时间段的会议开始时间
        Calendar currentStartTime = trackSession.getSessionStartTime();
        List<SessionTalkInfo> sessionTalkInfo = new ArrayList<>();
        for (Iterator<TalkInfo> talksIterator = talkInfoList.iterator(); talksIterator.hasNext();) {
            TalkInfo talkInfo = talksIterator.next();
            if (ConferenceUtils.hasFreeTimeForTalk(talkInfo,trackSession)) {
                // 将会议信息安排到对应时间段内
                sessionTalkInfo.add(new SessionTalkInfo(currentStartTime, talkInfo.getTalkTitle(),
                        talkInfo.getTalkTiemStr()+talkInfo.getTalkTimeUnit()));
                trackSession.setTotalCapacity(trackSession.getTotalCapacity()-talkInfo.getTalkTimeIntValue());
                // 计算下个会议安排的开始时间
                currentStartTime = ConferenceUtils.getNextStartTime(currentStartTime, talkInfo);
                // 移除已经安排的会议
                talksIterator.remove();
            }
        }
        return sessionTalkInfo;
    }

}
